package io.gitee.jinceon;

import io.gitee.jinceon.parser.SimpleTreeLexer;
import io.gitee.jinceon.parser.SimpleTreeParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.ArrayList;
import java.util.List;

public class SimpleTree {

    /**
     * 输入 a+b*c-d/e 之类的带变量的算式，返回计算结果
     * @param s
     * @return transformed string
     */
    public static String transform(String s) {
        SimpleTreeLexer lexer = new SimpleTreeLexer(CharStreams.fromString(s));
        SimpleTreeParser parser = new SimpleTreeParser(new CommonTokenStream(lexer));
        ParseTree tree = parser.parse();

        SimpleTreeVisitor visitor = new SimpleTreeVisitor();
        return visitor.visit(tree);
    }

    public static List<Node> flatten(String s) {
        SimpleTreeLexer lexer = new SimpleTreeLexer(CharStreams.fromString(s));
        SimpleTreeParser parser = new SimpleTreeParser(new CommonTokenStream(lexer));
        ParseTree tree = parser.parse();

        List<Node> result = new ArrayList<>();
        SimpleTreeFlattenVisitor visitor = new SimpleTreeFlattenVisitor(result);
        visitor.visit(tree);
        return result;
    }

}
